<?php
require('../fb-library/src/facebook.php');
require_once("config_root.php");
require_once("db_api.php");
date_default_timezone_set('Asia/Taipei') ;

if(!isset($_GET['place'])) die("should provide place");
$val = $_GET['place'];


$config=array();
$config['appId']='607470316008019';	
$config['secret']='07ee2e52439a998afc5c41aaff2baa71';	
$config['cookie']=true;	


/***************************************/

$facebook = new Facebook($config);
$totalCheckIns = 0;
if(isset($_GET['place'])){
	
	#$file = $_GET['place'];
	#$places = json_decode(file_get_contents("data/$file"),true);
	$places = query_tbl_sample($_GET['place']);

	$placeCnt= count($places);

	#for($i=0;$i<$placeCnt;$i++ ) {
	foreach($places as $key => $place) {
		$id = $place['id'];
		try {
			$page = $facebook->api("/$id");
		} catch (FacebookApiException $e) {
			echo "Get Page Error %e, ".__FILE__.__LINE__."\n";
		}
		if(isset($page['checkins'])) 
		{
			 $checkins = $page['checkins'];
			 $name = $page['name'];
			 #echo "$name: $id:  $checkins <BR>";
			 $totalCheckIns += $checkins;
			# $output[$i] = array(
			#	'name' => $name,
			#	'id' => $id,
			#	'checkins' => $checkins);
		}
		
		
	} 
	#$t=time();

	#$dstfile = "data/$file.data".date('Ymd');	
	#touch($dstfile);
	#$saveData = json_decode(file_get_contents($dstfile),true);
	#$saveData[date('G',$t)] = $totalCheckIns;
	$json_out['checkins'] = $totalCheckIns;
	echo json_encode($json_out);
	#file_put_contents($dstfile, json_encode($saveData));
	#echo json_encode($saveData);
	#echo json_encode($output);
	#echo "Total Checkins = $totalCheckIns <BR>";
	#if(isset($output))
	#$output['time'] = $t;
	#echo json_encode($output)."<BR>";
	
} else  {
	$json_out['checkins'] = -1;
	echo json_encode($json_out);
}
?>






